﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;

namespace DNA.Mvc.Management
{
    public interface ILogger
    {
        /// <summary>
        /// Write the information message to log.
        /// </summary>
        /// <param name="message">The message to write.</param>
        void Info(string message);

        /// <summary>
        /// Write the warnning message to log.
        /// </summary>
        /// <param name="message">The message to write.</param>
        void Warn(string message);
        
        /// <summary>
        /// Write error message to log.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="e"></param>
        void Error(string message, Exception e);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        void Error(Exception e);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        void Fatal(string message, Exception e);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        void Fatal(Exception e);

        //public static void Write(string category, string message, Exception e)
        //{
        //    var log = new LogEntry();
        //    if (string.IsNullOrEmpty(category))
        //    {
        //        if (e != null)
        //            log.Category = ERROR;
        //        else
        //            log.Category = INFO;
        //    }

        //    if (e != null)
        //    {
        //        if (string.IsNullOrEmpty(message))
        //            log.Message = e.Message;
        //        else
        //            log.Message = message;

        //        log.Detail = e.StackTrace;
        //        var context = HttpContext.Current;

        //        if (context != null)
        //        {
        //            if (context.Request != null)
        //                log.RequestUrl = context.Request.RawUrl;
        //            if ((context.User != null) && (context.User.Identity != null))
        //                log.UserName = context.User.Identity.Name;
        //        }
        //    }
        //}

        //internal static void Write(LogEntry entry)
        //{
        //    //Apply filters
        //    if (entry != null)
        //    {
        //        var writerRefs = Filters[entry.Category];
        //        Writers.Where(w => writerRefs.Contains(w.Key.ToUpper()))
        //            .Select(w => w.Value)
        //            .AsParallel()
        //            .ForAll(w => { w.Write(entry); });
        //    }
        //}

        //public static IEnumerable<LogEntry> Read(string logEventType)
        //{
        //    throw new NotImplementedException();
        //}

        //public static IEnumerable<LogEntry> Read(string logEventType, DateTime day)
        //{
        //    throw new NotImplementedException();
        //}

        //public static IEnumerable<LogEntry> Read(string logEventType, DateTime start, DateTime end)
        //{
        //    throw new NotImplementedException();
        //}

        //private static IDictionary<string, ILogWriter> writers;
        //private static IDictionary<string, string[]> filters;

        //public static IDictionary<string, ILogWriter> Writers
        //{
        //    get
        //    {
        //        if (writers == null)
        //            writers = new Dictionary<string, ILogWriter>();
        //        return writers;
        //    }
        //}

        //public static IDictionary<string, string[]> Filters
        //{
        //    get
        //    {
        //        if (filters == null)
        //            filters = new Dictionary<string, string[]>();
        //        return filters;
        //    }
        //}

        //public static void Configure(LoggingConfigurationSection section)
        //{
        //    Writers.Clear();
        //    Filters.Clear();

        //    for (int i = 0; i < section.Writers.Count; i++)
        //    {
        //        var settings = section.Writers[i];
        //        var type = Type.GetType(settings.Type);

        //        if (type == null)
        //            throw new Exception("Type not found.");

        //        var instance = Activator.CreateInstance(type) as ILogWriter;
        //        if (instance == null)
        //            throw new Exception(string.Format("The writer \"{0}\" could not be created.", settings.Type));

        //        Writers.Add(settings.Name, instance);
        //    }

        //    for (int i = 0; i < section.Filters.Count; i++)
        //    {
        //        var filterSettings = section.Filters[i];
        //        var writerRefs = new List<string>();
        //        for (int j = 0; j < filterSettings.Writers.Count; j++)
        //            writerRefs.Add(filterSettings.Writers[j].Name);
        //        Filters.Add(filterSettings.Category, writerRefs.ToArray());
        //    }
        //}
    }

}
